package cn.org.eshow.webapp.action;

import cn.org.eshow.bean.query.WithdrawQuery;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.model.Money;
import cn.org.eshow.model.User;
import cn.org.eshow.model.Withdraw;
import cn.org.eshow.model.WithdrawAccount;
import cn.org.eshow.service.MoneyManager;
import cn.org.eshow.service.WithdrawAccountManager;
import cn.org.eshow.service.WithdrawManager;
import cn.org.eshow.util.OrderUtil;
import cn.org.eshow.webapp.action.response.WithdrawResponse;
import cn.org.eshow.webapp.util.RenderUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.AllowedMethods;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 提现记录API接口类
 */
@AllowedMethods({"save", "mine", "search"})
public class WithdrawAction extends ApiBaseAction<Withdraw> {

    private static final long serialVersionUID = 1L;

    @Autowired
    private WithdrawManager withdrawManager;
    @Autowired
    private MoneyManager moneyManager;
    @Autowired
    private WithdrawAccountManager withdrawAccountManager;

    private Withdraw withdraw = new Withdraw();
    private List<Withdraw> withdraws;
    private WithdrawQuery query = new WithdrawQuery();
    public String code;//验证码
    public String mobile;//手机号码

    /**
     * 查询所有提现记录
     */
    public void search() throws Exception {
        Page<Withdraw> page = withdrawManager.search(query);
        if (page.getDataList().isEmpty()) {
            RenderUtil.failure("暂无数据");
            return;
        }
        List<WithdrawResponse> results = new ArrayList<WithdrawResponse>();
        for (Withdraw obj : page.getDataList()) {
            WithdrawResponse response = new WithdrawResponse(obj);
            if (!results.contains(response)) {
                results.add(response);
            }
        }
        RenderUtil.page("withdraws", "获取成功", page, results);
    }

    /**
     * 我的提现记录
     */
    public void mine() throws Exception {
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        query.setUserId(user.getId());
        Page<Withdraw> page = withdrawManager.search(query);
        if (page.getDataList().isEmpty()) {
            RenderUtil.failure("暂无数据");
            return;
        }
        List<WithdrawResponse> results = new ArrayList<WithdrawResponse>();
        for (Withdraw obj : page.getDataList()) {
            WithdrawResponse response = new WithdrawResponse(obj);
            if (!results.contains(response)) {
                results.add(response);
            }
        }
        RenderUtil.page("withdraws", "获取成功", page, results);
    }

    /**
     * 申请提现(目前微信提现不做)
     */
    public void save() throws Exception {
        if (StringUtils.isEmpty(withdraw.getWay()) || withdraw.getPrice() == null) {
            RenderUtil.failure("参数不能为空");
            return;
        }
        if (withdraw.getPrice() < 50) {
            RenderUtil.failure("提现金额小于50，不能提现！");
            return;
        }
        if (withdraw.getPrice() % 10 != 0) {
            RenderUtil.failure("请输入整数");
            return;
        }
        User user = accessTokenManager.isValid(accessToken);
        if (user == null) {
            RenderUtil.expires();//用户信息过期
            return;
        }
        Money money = moneyManager.mine(user);
        if (money.getBalance() < withdraw.getPrice()) {
            RenderUtil.failure("提现金额大于自己账户金额，不能提现！");
            return;
        }
        withdraw.setNo(OrderUtil.getOrderNo());
        withdraw.setUser(user);

        WithdrawAccount old = withdrawAccountManager.mine(user);//获取我的提现账号信息
        if (withdraw.getWay().equals("银行卡")) {
            if (StringUtils.isEmpty(old.getBankCardNo()) || StringUtils.isEmpty(old.getBankName()) || StringUtils.isEmpty(old.getBankAccountName())) {
                RenderUtil.failure("请先绑定银行卡");
                return;
            }
            withdraw.setChannel(old.getBankName());
            withdraw.setName(old.getBankAccountName());
            withdraw.setAccount(old.getBankCardNo());
        } else if (withdraw.getWay().equals("支付宝")) {
            if (StringUtils.isEmpty(old.getAlipayAccount()) || StringUtils.isEmpty(old.getAlipayName())) {
                RenderUtil.failure("请先绑定支付宝");
                return;
            }
            withdraw.setChannel("支付宝");
            withdraw.setName(old.getAlipayName());
            withdraw.setAccount(old.getAlipayAccount());
        } else {
            RenderUtil.failure("非法提现方式");
            return;
        }
        withdraw = withdrawManager.save(withdraw);

        if (!moneyManager.withdraw(money, withdraw)) {
            RenderUtil.failure("提现失败");
            return;
        }

        RenderUtil.success("申请提现成功");
    }

    public Withdraw getWithdraw() {
        return withdraw;
    }

    public void setWithdraw(Withdraw withdraw) {
        this.withdraw = withdraw;
    }

    public List<Withdraw> getWithdraws() {
        return withdraws;
    }

    public void setWithdraws(List<Withdraw> withdraws) {
        this.withdraws = withdraws;
    }

    public WithdrawQuery getQuery() {
        return query;
    }

    public void setQuery(WithdrawQuery query) {
        this.query = query;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
